
import { _decorator, Component, Node, sys } from 'cc';
const { ccclass, property } = _decorator;
/**
 * 本地存储工具类
 * 带有缓存功能，不会每次都从本地序列化的数据中读取
 */
@ccclass('settingUtil')
export class settingUtil {
    public static getInt(k: string, defaultValue: number) {
        var v = this.getValue(k, defaultValue);
        return parseInt("" + v);
    }

    public static getFloat(k: string, defaultValue: number) {
        var v = this.getValue(k, defaultValue);
        return parseFloat("" + v);
    }

    public static getBool(k: string, defaultValue: boolean) {
        let defaultValueTemp: string;
        if (defaultValue == true) {
            defaultValueTemp = 'true';
        } else {
            defaultValueTemp = 'false';
        }
        var v = this.getString(k, defaultValueTemp);
        if (v == 'true') {
            return true;
        }
        return false;
    }

    public static getString(k: string, defaultValue: string) {
        return this.getValue(k, defaultValue);
    }

    public static getObject(k: string, defaultValue: Object) {
        var v = this.getValue(k, "");
        if (!v || v == '') {
            return defaultValue;
        }
        return JSON.parse(v);
    }

    //---------------------------------------------
    /**
     * 
     * @param k 
     * @param v 
     * @param isSaveToLocal 是否存储到本地
     */
    public static setObject(k: string, v: Object, isSaveToLocal: boolean = false) {
        let vStr: string = '';
        if (v) {
            vStr = JSON.stringify(v);
        }
        this.setValue(k, vStr, isSaveToLocal);
    }
    /**
     * 
     * @param k 
     * @param v 
     * @param isSaveToLocal 是否存储到本地
     */
    public static setBool(k: string, v: boolean, isSaveToLocal: boolean = false) {
        if (v == true) {
            this.setValue(k, 'true', isSaveToLocal);
        } else {
            this.setValue(k, 'false', isSaveToLocal);
        }
    }

    /**
     * 追加整数形
     * @param k 
     * @param v 
     * @param isSaveToLocal 是否存储到本地
     * @returns 
     */
    public static appendInt(k: string, v: any, isSaveToLocal: boolean = false) {
        let vint = this.getInt(k, 0);
        let v2Save = parseInt(v) + vint;
        this.setValue(k, v2Save, isSaveToLocal);
        return v2Save;
    }
    /**
    * 
    * @param k 
    * @param v 
    * @param isSaveToLocal 是否存储到本地
    * @returns 
    */
    public static appendFloat(k: string, v: any, isSaveToLocal: boolean = false) {
        let vf = this.getFloat(k, 0);
        let v2Save = parseFloat(v) + vf;
        this.setValue(k, v2Save, isSaveToLocal);
    }

    //-------------------------------------------------

    public static removeValueOfKey(key: string) {
        sys.localStorage.removeItem(key);
        if (this.memoryCache.hasOwnProperty(key)) {
            delete this.memoryCache[key];
        }
    }

    public static removeAll() {
        sys.localStorage.clear();
        this.memoryCache = {};
    }
    /**
     * 更新数据
     * @param k 
     * @param v 
     * @param isSaveToLocal 是否存储到本地
     */
    public static setValue(k: string, v: any, isSaveToLocal: boolean) {
        if (isSaveToLocal) {
            if (window["wx"]) {
                window["wx"].setStorage({
                    key: k,
                    data: v,
                    success: () => {
                        console.warn("setValue success ", k);
                    },
                    fail: () => {
                        console.warn("setValue fail", k, v);
                    }
                });
            }
            else {
                sys.localStorage.setItem(k, v);
            }
        }
        this.memoryCache[k] = v;
    }
    //-------------------------------------------------
    private static memoryCache: { [key: string]: any } = {};
    /**
     * 从本地存储中读取数据，只有第一次会从本地读取，之后会从内存中读取
     * @param k 
     * @param defaultValue 
     * @returns 
     */
    public static getValue(k: string, defaultValue: any): string {
        if (this.memoryCache.hasOwnProperty(k)) {
            return this.memoryCache[k];
        }
        else {
            let value = sys.localStorage.getItem(k);
            if (value == null || value == '') {
                value = defaultValue;
            }
            this.memoryCache[k] = value;
            return value;
        }
    }
}
